Page({
    data: {
      avatarUrl: '/images/default-avatar.png', // 默认头像，需在images目录下放置默认头像图片
      nickname: '',
      studentId: '2022010101', // 示例学号
      showNicknameModal: false,
      newNickname: ''
    },
  
    onLoad() {
      // 从本地存储获取用户信息（登录时存储）
      const userInfo = wx.getStorageSync('userInfo') || {};
      this.setData({
        avatarUrl: userInfo.avatarUrl || this.data.avatarUrl,
        nickname: userInfo.nickname
      });
    },
  
    // 选择头像
    chooseAvatar() {
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: (res) => {
          const tempFilePath = res.tempFilePaths[0];
          this.setData({
            avatarUrl: tempFilePath
          });
          // 这里可将头像地址上传到服务器，实际开发需补充
          let userInfo = wx.getStorageSync('userInfo') || {};
          userInfo.avatarUrl = tempFilePath;
          wx.setStorageSync('userInfo', userInfo);
        }
      });
    },
  
    // 编辑昵称，显示弹窗
    editNickname() {
      this.setData({
        showNicknameModal: true,
        newNickname: this.data.nickname || ''
      });
    },
  
    // 输入新昵称
    inputNewNickname(e) {
      this.setData({
        newNickname: e.detail.value
      });
    },
  
    // 取消修改昵称
    cancelEditNickname() {
      this.setData({
        showNicknameModal: false,
        newNickname: ''
      });
    },
  
    // 确认修改昵称
    confirmEditNickname() {
      if (!this.data.newNickname) {
        wx.showToast({
          title: '昵称不能为空',
          icon: 'none'
        });
        return;
      }
      this.setData({
        nickname: this.data.newNickname,
        showNicknameModal: false
      });
      // 存储新昵称到本地
      let userInfo = wx.getStorageSync('userInfo') || {};
      userInfo.nickname = this.data.newNickname;
      wx.setStorageSync('userInfo', userInfo);
      wx.showToast({
        title: '修改成功',
        icon: 'success'
      });
    },
  
    // 前往我的发布记录
    goMyRecords() {
      wx.navigateTo({
        url: '/pages/myRecords/myRecords' // 需新建myRecords页面
      });
    },
  
    // 前往设置页面
    goSetting() {
      wx.navigateTo({
        url: '/pages/setting/setting' // 需新建setting页面
      });
    },
  
    // 退出登录
    logout() {
      wx.showModal({
        title: '提示',
        content: '确定要退出登录吗？',
        success: (res) => {
          if (res.confirm) {
            wx.removeStorageSync('isLogin');
            wx.removeStorageSync('userInfo');
            wx.redirectTo({
              url: '/pages/login/login' // 跳转到登录页面
            });
          }
        }
      });
    }
  });